﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrokerBase.Lib.Core;
using BrokerBase.Lib.Tool;
using System.Reflection;
using BrokerBase.Lib.Product.RateFactorConverter;

namespace BrokerBase.Lib.Product
{
    public class RateFactor:IPK,IRateFactor
    {
        public const string  RowRateFactorDefaultName1 = "Empty1";

        public const string RowRateFactorDefaultName2 = "Empty2";

        private IRateFactorConverter converter;

        public RateFactor()
        {

        }

        public RateFactor(IProduct product,RateFactorAxis axis):this()
        {
            Axis = axis;
            Owner = product;
        }

        public virtual int Id { get; set; }

        public virtual string Name { get; set; }

        public virtual IProduct Owner{get;set;}

        public virtual RateFactorAxis Axis{get;set;}

        public virtual string Items{get;set;}

        public virtual string[] GroupItems
        {
            get
            {
                return Items.Split(';').GroupBy(p => p).Select(g => g.Key).Where(k => !string.IsNullOrEmpty(k)).ToArray<string>();
            }
        }

        public virtual string ConverterClassFullName { get; set; }

        public virtual string ConverterAssemblyFullName { get; set; }

        public virtual bool IsHasConverter
        {
            get { return !string.IsNullOrEmpty(ConverterClassFullName); }
        }

        public virtual bool IsLadderConverter
        {
            get
            {
                return IsHasConverter && ConverterInstance.IsLadder ;
            }
        }

        public virtual IRateFactorConverter ConverterInstance 
        {
            get
            {

                if (converter == null)
                {
                    Assembly assembly = Assembly.LoadFrom(ConverterAssemblyFullName);

                    object obj = assembly.CreateInstance(ConverterClassFullName);

                    converter = obj as IRateFactorConverter;
                    converter.Owner = this;
                }

                return converter;
            }
            set
            {
                
                converter = value;

                if (converter != null)
                {
                    converter.Owner = this;
                    Type t = converter.GetType();
                    ConverterClassFullName = t.FullName;
                    ConverterAssemblyFullName = t.Module.Name;
                }
            }
        }

        public virtual bool IsEmpty { get { return Axis==RateFactorAxis.Row && ( Name==RowRateFactorDefaultName1 || Name==RowRateFactorDefaultName2) ;} }

        public virtual bool IsDisplay 
        { 
            get 
            {
                if (IsEmpty)
                    return false;
                else
                    if (!IsHasConverter)
                        return true;
                    else
                        return ConverterInstance.IsDisplay;
            } 
        }

        public virtual string GetDefaultItem()
        {
            string[] ss = GroupItems;

            if (ss.Count() > 0)
                return GroupItems[0];
            else
                return string.Empty;
        }
    }
}
